﻿
using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;

using DotNetNuke.Common;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;



namespace DotNetNuke.Modules.UserDefinedTable
{


  public class DataTypeBoolean : DataType
  {


    public override void RenderValuesToHtmlInsideDataSet(DataSet ds, int ModuleId, bool noScript)
    {
      ArrayList BooleanFields = new ArrayList();
      DataTable table = ds.Tables[DataSetTableName.Data];

      string title;

      foreach (DataRow current in ds.Tables["Fields"].Rows)
      {
        if (current[FieldsTableColumn.Type].ToString() == "Boolean")
        {
          title = (string)current[FieldsTableColumn.Title];
          BooleanFields.Add(title);
          table.Columns.Add(new DataColumn(title + DataTableColumn.Appendix_Original, typeof(string)));
          table.Columns.Add(new DataColumn(title + DataTableColumn.Appendix_LocalizedValue, typeof(string)));
          table.Columns.Add(new DataColumn(title + DataTableColumn.Appendix_Caption, typeof(string)));
        }
      }

      if ((ds != null) & (BooleanFields.Count > 0))
      {
        ModuleController controller = new ModuleController();
        string strFieldvalue, alt, strBoolean;

        foreach(DataRow row in ds.Tables["Data"].Rows)
        {
          foreach (string FieldName in BooleanFields)
          {
            //if (row[FieldName] is bool)
            //{
            //  bool b = (bool)row[FieldName];
            //  alt = DotNetNuke.Services.Localization.Localization.GetString(b ? "Yes" : "No");

            //  if (b)
            //    strFieldvalue = string.Format("<img src=\"{0}/images/checked.gif\" alt=\"{1}\"/>", DotNetNuke.Common.Globals.ApplicationPath, alt);

            //  else
            //    strFieldvalue = string.Format("<img src=\"{0}/images/unchecked.gif\" alt=\"{1}\"/>", DotNetNuke.Common.Globals.ApplicationPath, alt);

            //}
            //else
            //{
            //  alt = DotNetNuke.Services.Localization.Localization.GetString("No");
            //  strFieldvalue = "";
            //}

            strBoolean = row[FieldName].ToString().ToLowerInvariant();
            alt = DotNetNuke.Services.Localization.Localization.GetString(Utilities.CBoolN(strBoolean, false) ? "Yes" : "No");

            if (strBoolean == "true")
              strFieldvalue = string.Format("<img src=\"{0}/images/checked.gif\" alt=\"{1}\"/>", DotNetNuke.Common.Globals.ApplicationPath, alt);

            else if (strBoolean == "false")
              strFieldvalue = string.Format("<img src=\"{0}/images/unchecked.gif\" alt=\"{1}\"/>", DotNetNuke.Common.Globals.ApplicationPath, alt);

            else
              strFieldvalue = "";

            row[FieldName] = strFieldvalue;
            row[FieldName + DataTableColumn.Appendix_Original] = strBoolean;
            row[FieldName + DataTableColumn.Appendix_Caption] = strBoolean;
          }
        }
      }
    }




    public override void SetStylesAndFormats(BoundField Column, string Format)
    {
      Column.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
      Column.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    }



    public override string Name
    {
      get { return "Boolean"; }
    }



    public override bool SupportsDefaultValue
    {
      get { return true; }
    }



    public override bool SupportsEditing
    {
      get { return true; }
    }



    public override bool SupportsEditStyle
    {
      get { return true; }
    }



    public override bool SupportsOutputSettings
    {
      get { return true; }
    }




  }

}

